package com.roobo.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.roobo.mqttsdk.manager.PlayControllerManager;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 播放控制
 */


public class PlayActivity extends AppCompatActivity implements View.OnClickListener, MyMediaPlayer.PlayStateListener {

    public static final String TAG = PlayActivity.class.getSimpleName();
    public static final String MESSAGE = "message";
    public static final String CMD_TAG = "cmd";
    public static final String CMD_FORWARD = "forward";
    public static final String CMD_DEMAND_MUSIC_ONLINE = "demandMusicOnline";
    public static final String CMD_BACKWARD = "backward";
    public static final String CMD_PAUSE = "pause";
    public static final String CMD_RESUME = "resume";
    public static final String URL = "url";
    public static final String TRACK_LIST_ID = "trackListId";
    public static final String TRACK_ID = "trackId";

    private TextView mPlayTnfoTv;
    private Button mNextBtn;
    private Button mPreviousBtn;
    private Button mPauseBtn;
    private Button mResumeBtn;
    private Button mPlayCollectionBtn;

    private String mTrackListId;
    private String mTrackId;
    private String mUrl;


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        playMedia(intent);
    }

    private void playMedia(Intent intent) {
        String message = intent.getStringExtra(MESSAGE);
        Log.d(TAG, "playMedia message:" + message);
        mPlayTnfoTv.setText(message);
        if (!TextUtils.isEmpty(message)) {
            try {
                JSONObject object = new JSONObject(message);
                String cmd = object.getString(CMD_TAG);
                if (CMD_FORWARD.equals(cmd) || CMD_DEMAND_MUSIC_ONLINE.equals(cmd) || CMD_BACKWARD.equals(cmd)) {//切换到歌曲之前，需要先停止播放当前歌曲，并上报状态“PLAY_STATE_STOP
                    if (!TextUtils.isEmpty(mTrackId)) {
                        PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_STOP, "", PlayControllerManager.PlayErrorType.PLAY_NO_ERROR);
                        PlayControllerManager.getInstance().sendPlayState(info);
                    }
                    mTrackListId = object.getString(TRACK_LIST_ID);
                    mTrackId = object.getString(TRACK_ID);
                    mUrl = object.getString(URL);
                    MyMediaPlayer.getInstance().prepare(mUrl);
                } else if (CMD_PAUSE.equals(cmd)) {//暂停
                    pause();

                } else if (CMD_RESUME.equals(cmd)) {//恢复
                    resume();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d(TAG, "playMedia e.getLocalizedMessage:" + e.getLocalizedMessage());
                Toast.makeText(this, getResources().getString(R.string.play_fail), Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.d(TAG, "playMedia message为空");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        setTitle(getResources().getString(R.string.play_activity_title));
        initViews();
        MainActivity.applyPermission(this);
        MyMediaPlayer.getInstance().setPlayStateListener(this);
        playMedia(getIntent());
    }

    private void initViews() {
        mPlayTnfoTv = findViewById(R.id.play_info_tv);
        mNextBtn = findViewById(R.id.next_btn);
        mPreviousBtn = findViewById(R.id.previous_btn);
        mPauseBtn = findViewById(R.id.pause_btn);
        mResumeBtn = findViewById(R.id.resume_btn);
        mPlayCollectionBtn = findViewById(R.id.play_collection);

        mNextBtn.setOnClickListener(this);
        mPreviousBtn.setOnClickListener(this);
        mPauseBtn.setOnClickListener(this);
        mResumeBtn.setOnClickListener(this);
        mPlayCollectionBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_btn:
                PlayControllerManager.getInstance().sendPlayCmd(PlayControllerManager.PlayCmd.SONG_PLAY_NEXT);
                break;
            case R.id.previous_btn:
                PlayControllerManager.getInstance().sendPlayCmd(PlayControllerManager.PlayCmd.SONG_PLAY_PREVIOUS);
                break;
            case R.id.pause_btn:
                pause();
                break;
            case R.id.resume_btn:
                resume();
                break;
            case R.id.play_collection:
                PlayControllerManager.getInstance().sendPlayCmd(PlayControllerManager.PlayCmd.SONG_PLAY_COLLECTION);
                break;
        }
    }

    @Override
    public void onPrepare() {
        if (!TextUtils.isEmpty(mTrackId)) {
            PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_READY, "", PlayControllerManager.PlayErrorType.PLAY_NO_ERROR);
            PlayControllerManager.getInstance().sendPlayState(info);
        }
    }

    @Override
    public void onPlaying() {
        if (!TextUtils.isEmpty(mTrackId)) {
            PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_PLAY, "", PlayControllerManager.PlayErrorType.PLAY_NO_ERROR);
            PlayControllerManager.getInstance().sendPlayState(info);
        }
    }

    @Override
    public void onPlayCompletion() {
        if (!TextUtils.isEmpty(mTrackId)) {
            PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_STOP, "", PlayControllerManager.PlayErrorType.PLAY_NO_ERROR);
            PlayControllerManager.getInstance().sendPlayState(info);
        }
    }

    @Override
    public void onPlayError(String error) {
        if (!TextUtils.isEmpty(mTrackId)) {
            PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_STOP, error, PlayControllerManager.PlayErrorType.PLAY_ERR_UNKNOWN);
            PlayControllerManager.getInstance().sendPlayState(info);
        }
    }

    private void pause() {
        if (!TextUtils.isEmpty(mTrackId)) {
            MyMediaPlayer.getInstance().pause();
            PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_PAUSE, "", PlayControllerManager.PlayErrorType.PLAY_NO_ERROR);
            PlayControllerManager.getInstance().sendPlayState(info);
        }
    }

    private void resume() {
        if (!TextUtils.isEmpty(mTrackId)) {
            MyMediaPlayer.getInstance().resume();
            PlayControllerManager.PlayStateInfo info = createStateInfo(mTrackListId, mTrackId, PlayControllerManager.PlayStateType.PLAY_STATE_PLAY, "", PlayControllerManager.PlayErrorType.PLAY_NO_ERROR);
            PlayControllerManager.getInstance().sendPlayState(info);
        }
    }

    public static PlayControllerManager.PlayStateInfo createStateInfo(String trackListId, String trackId, PlayControllerManager.PlayStateType stateType, String error, PlayControllerManager.PlayErrorType errorType) {
        PlayControllerManager.PlayStateInfo info = new PlayControllerManager.PlayStateInfo();
        info.setState(stateType);
        info.setTrackListId(trackListId);
        info.setTrackId(trackId);
        info.setReason(error);
        info.setOffset(1);
        info.setError(errorType);
        return info;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyMediaPlayer.getInstance().release();
    }
}
